package com.hui.app;

import com.hui.utils.DateUtil;
import com.hui.utils.DruidUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.junit.Test;

import java.sql.SQLException;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: 李昭辉
 * @Date: 2021/06/14/9:32
 * @Description:
 */
public class AccountApp {
    // 实现卡号：1122334455向55443332211转账5000元的操作
    @Test
    public void transfer() throws SQLException {
        String c1 = "1122334455";
        String c2 = "55443332211";
        double money = 5000;
        QueryRunner qr = new QueryRunner(DruidUtil.getDataSource());
        // 先查询1122334455的余额,如果不足就直接退出
        double query = qr.query("select balance from account where card = ?", new ScalarHandler<>(), c1);
        if (query < money) {
            System.out.println("余额不足!");
            return;
        }
        // 开始转账
        String sql = "update account set balance=balance + ? where card = ?";
        qr.update(sql, -money, c1);
        qr.update(sql, money, c2);
        // 添加记录
        String sql2 = "insert into transaction values(null,?,?,?,?)";
        qr.update(sql2, c1, "转出", money, DateUtil.getDate());
        qr.update(sql2, c2, "转入", money, DateUtil.getDate());
        System.out.println("转账成功 ！！！ ");
    }


}
